//1.产品
class BOOK {
    constructor(){
        this.book = []
    }
    page(content){
        return  `************\n************\n*****${content}****\n************\n************\n`
    }
    addPage(page){
        this.book.push(page)
    }
    showBook(){
        for(let i =0 ; i<this.book.length;i++)console.log(this.book[i]);
    }
}
//2.定义建造者接口
class Builder{
    //添加目录
    addCataLog(){
        throw new Error('必须实现 addCataLog方法')
    }
    //添加内容
    addContent(){
        throw new Error('必须实现 addContent 方法')
    }
    //添加结尾
    addEnd(){
        throw new Error('必须实现 addEnd 方法')
    }
    //交付产品
    getResult(){
        throw new Error('必须实现 getResult 方法')
    }
}
//3.具体实现建造者
class BookBuilder extends Builder{
    constructor(){
        super()
        this.product = new BOOK()
    }
    addCataLog(){
        this.product.addPage(this.product.page('目录'))
    }
    addContent(content){
        this.product.addPage(this.product.page(content))

    }
    addEnd(){
        this.product.addPage(this.product.page('结尾'))
    }
    getResult(){
        return this.product
    }
}
//4.定义指挥者
class Director{
    setBuilder(builder){
        this.builder = builder
    }
    construct(){
        this.builder.addCataLog()
        this.builder.addContent('内容1')
        this.builder.addContent('内容2')
        this.builder.addEnd()
    }
}
//5.客户端
const  director =new Director()
const builder = new BookBuilder()
director.setBuilder(builder)
director.construct()
const product = builder.getResult()
product.showBook()